/**
 * 
 */
package three_star_craft.uap.state;

import org.grlea.log.SimpleLogger;

import three_star_craft.uap.UapGenerator;
import three_star_craft.uap.event.EventUapConnectionCfm;
import three_star_craft.uap.event.EventUapConnectionRej;
import three_star_craft.uap.timer.TimerEvent;

/**
 * State of UapGenerator just after sending of UapConnectionReq message.
 * Timer tWaitForUapConCfm is running.
 * 
 * @author w.antosiewic
 *
 */
public class UapStateConnectPend extends UapStateBase
{
    private static final SimpleLogger logger = new SimpleLogger(UapStateConnectPend.class);
    
    private static UapStateConnectPend instance = new UapStateConnectPend();
    
    /**
     * Return singleton instance of this state
     * @return an instance of this state
     */
    public static UapStateConnectPend getInstance()
    {
        return instance;
    }
    
    private UapStateConnectPend()
    {
        super("ConnectPend");
    }
    
	/* (non-Javadoc)
	 * @see three_star_craft.uap.state.UapStateBase#handleUapTimerWaitForConnectionCfm(three_star_craft.uap.timer.TimerEvent)
	 */
	@Override
	public void handleUapTimerWaitForConnectionCfm(UapGenerator generator,
	                                               TimerEvent event)
	{
	    logger.debugObject("Timer tWaitForConnectionCfm expired for generator:", generator.getDebugInfo());
	    generator.changeState(UapStateMap.UAP_IDLE);
	    generator.getUapConnectionListener().
	        connectionEstablishTimeout(generator);
	}

	/* (non-Javadoc)
	 * @see three_star_craft.uap.state.UapStateBase#handleUapConnectionReject(three_star_craft.uap.event.EventUapConnectionRej)
	 */
	@Override
	public void handleUapConnectionReject(UapGenerator generator,
	                                      EventUapConnectionRej event)
	{
	    generator.cancelTWaitForConnCfm();
	    generator.changeState(UapStateMap.UAP_IDLE);
	    // inform listener about failed attempt
	    generator.getUapConnectionListener().
	        connectionEstablishFailInd(generator, event.getCause());
	}

	/* (non-Javadoc)
	 * @see three_star_craft.uap.state.UapStateBase#handleUapConnectionConfirm(three_star_craft.uap.event.EventUapConnectionCfm)
	 */
	@Override
	public void handleUapConnectionConfirm(UapGenerator generator,
	                                       EventUapConnectionCfm event)
	{
	    generator.cancelTWaitForConnCfm();
	    
	    generator.changeState(UapStateMap.UAP_CONNECTED);
	    
	    // inform listener about successful attempt
	    generator.getUapConnectionListener().connectionEstablishSuccessInd(
	            generator,
	            event.getPlayableItems());
	}

}
